package com.insyte.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.insyte.R;
import com.insyte.fragments.ProfileFavourisFragment;
import com.insyte.model.CategoryOfInterest;
import com.insyte.model.InteresetObject;

import java.util.List;

/**
 * Created by Ngo Khanh on 26/6/2015.
 */
public class ExpandableInterestAdapter extends BaseExpandableListAdapter {

    private static final class ViewHolder {
        TextView textLabel;
        Button btn_add_to_interest;
    }

    private final List<CategoryOfInterest> itemList;
    private final LayoutInflater inflater;
    ProfileFavourisFragment.OnChildItemListener itemListener;

    public ExpandableInterestAdapter(Context context, List<CategoryOfInterest> itemList, ProfileFavourisFragment.OnChildItemListener listener) {
        this.inflater = LayoutInflater.from(context);
        this.itemList = itemList;
        this.itemListener = listener;
    }

    @Override
    public InteresetObject getChild(int groupPosition, int childPosition) {

        return itemList.get(groupPosition).getChildItemList().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return itemList.get(groupPosition).getChildItemList().size();
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
                             final ViewGroup parent) {
        View resultView = convertView;
        ViewHolder holder;
        if (resultView == null) {
            resultView = inflater.inflate(R.layout.row_child_interest, null); //TODO change layout id
            holder = new ViewHolder();
            holder.textLabel = (TextView) resultView.findViewById(R.id.tv_title_child); //TODO change view id
            holder.btn_add_to_interest = (Button) resultView.findViewById(R.id.btn_add_to_interest);
            resultView.setTag(holder);
        } else {
            holder = (ViewHolder) resultView.getTag();
        }
        final InteresetObject item = itemList.get(groupPosition).getChildItemList().get(childPosition);
        holder.btn_add_to_interest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemListener != null) {
                    itemListener.onClick(item);
                }
            }
        });
        holder.textLabel.setText(item.getName());
        return resultView;
    }

    public void removeChild(int groupPosition, int childPosition) {
        if (getChildrenCount(groupPosition) > 0 && getChildrenCount(groupPosition) - 1 >= childPosition) {
            itemList.get(groupPosition).getChildItemList().remove(childPosition);
            this.notifyDataSetChanged();
        }
    }

    @Override
    public CategoryOfInterest getGroup(int groupPosition) {
        return itemList.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return itemList.size();
    }

    @Override
    public long getGroupId(final int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View theConvertView, ViewGroup parent) {
        View resultView = theConvertView;
        ViewHolder holder;
        if (resultView == null) {
            resultView = inflater.inflate(R.layout.row_interest, null); //TODO change layout id
            holder = new ViewHolder();
            holder.textLabel = (TextView) resultView.findViewById(R.id.tv_title); //TODO change view id
            resultView.setTag(holder);
        } else {
            holder = (ViewHolder) resultView.getTag();
        }
        final CategoryOfInterest item = itemList.get(groupPosition);

        holder.textLabel.setText(item.getName());
        return resultView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}